package com.eva.service.system.impl;

import com.eva.core.model.PageData;
import com.eva.core.model.PageWrap;
import com.eva.core.utils.Utils;
import com.eva.dao.system.SystemJobLogMapper;
import com.eva.dao.system.model.SystemJobLog;
import com.eva.service.system.SystemJobLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 系统定时任务日志Service实现
 * @author Eva.Caesar Liu
 * @date 2022/03/05 10:30
 */
@Service
public class SystemJobLogServiceImpl implements SystemJobLogService {

    @Autowired
    private SystemJobLogMapper systemJobLogMapper;

    @Override
    public Integer create(SystemJobLog systemJobLog) {
        systemJobLogMapper.insert(systemJobLog);
        return systemJobLog.getId();
    }

    @Override
    public void deleteById(Integer id) {
        systemJobLogMapper.deleteById(id);
    }

    @Override
    public void delete(SystemJobLog systemJobLog) {
        UpdateWrapper<SystemJobLog> deleteWrapper = new UpdateWrapper<>(systemJobLog);
        systemJobLogMapper.delete(deleteWrapper);
    }

    @Override
    public void deleteByIdInBatch(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return;
        }
        systemJobLogMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateById(SystemJobLog systemJobLog) {
        systemJobLogMapper.updateById(systemJobLog);
    }

    @Override
    public void updateByIdInBatch(List<SystemJobLog> systemJobLogs) {
        if (CollectionUtils.isEmpty(systemJobLogs)) {
            return;
        }
        for (SystemJobLog systemJobLog: systemJobLogs) {
            this.updateById(systemJobLog);
        }
    }

    @Override
    public SystemJobLog findById(Integer id) {
        return systemJobLogMapper.selectById(id);
    }

    @Override
    public SystemJobLog findOne(SystemJobLog systemJobLog) {
        QueryWrapper<SystemJobLog> wrapper = new QueryWrapper<>(systemJobLog);
        return systemJobLogMapper.selectOne(wrapper);
    }

    @Override
    public List<SystemJobLog> findList(SystemJobLog systemJobLog) {
        QueryWrapper<SystemJobLog> wrapper = new QueryWrapper<>(systemJobLog);
        return systemJobLogMapper.selectList(wrapper);
    }
  
    @Override
    public PageData<SystemJobLog> findPage(PageWrap<SystemJobLog> pageWrap) {
        IPage<SystemJobLog> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
        QueryWrapper<SystemJobLog> queryWrapper = new QueryWrapper<>();
        Utils.MP.blankToNull(pageWrap.getModel());
        if (pageWrap.getModel().getJobId() != null) {
            queryWrapper.lambda().eq(SystemJobLog::getJobId, pageWrap.getModel().getJobId());
        }
        if (pageWrap.getModel().getSnippetCode() != null) {
            queryWrapper.lambda().eq(SystemJobLog::getSnippetCode, pageWrap.getModel().getSnippetCode());
        }
        if (pageWrap.getModel().getBusinessTime() != null) {
            queryWrapper.lambda().ge(SystemJobLog::getBusinessTime, Utils.Date.getStart(pageWrap.getModel().getBusinessTime()));
            queryWrapper.lambda().le(SystemJobLog::getBusinessTime, Utils.Date.getEnd(pageWrap.getModel().getBusinessTime()));
        }
        if (pageWrap.getModel().getHandleSuccessSize() != null) {
            queryWrapper.lambda().eq(SystemJobLog::getHandleSuccessSize, pageWrap.getModel().getHandleSuccessSize());
        }
        if (pageWrap.getModel().getServerIp() != null) {
            queryWrapper.lambda().eq(SystemJobLog::getServerIp, pageWrap.getModel().getServerIp());
        }
        if (pageWrap.getModel().getTriggerType() != null) {
            queryWrapper.lambda().eq(SystemJobLog::getTriggerType, pageWrap.getModel().getTriggerType());
        }
        if (pageWrap.getModel().getRemark() != null) {
            queryWrapper.lambda().eq(SystemJobLog::getRemark, pageWrap.getModel().getRemark());
        }
        if (pageWrap.getModel().getCreateTime() != null) {
            queryWrapper.lambda().ge(SystemJobLog::getCreateTime, Utils.Date.getStart(pageWrap.getModel().getCreateTime()));
            queryWrapper.lambda().le(SystemJobLog::getCreateTime, Utils.Date.getEnd(pageWrap.getModel().getCreateTime()));
        }
        for(PageWrap.SortData sortData: pageWrap.getSorts()) {
            if (sortData.getDirection().equalsIgnoreCase(PageWrap.DESC)) {
                queryWrapper.orderByDesc(sortData.getProperty());
            } else {
                queryWrapper.orderByAsc(sortData.getProperty());
            }
        }
        return PageData.from(systemJobLogMapper.selectPage(page, queryWrapper));
    }

    @Override
    public long count(SystemJobLog systemJobLog) {
        QueryWrapper<SystemJobLog> wrapper = new QueryWrapper<>(systemJobLog);
        return systemJobLogMapper.selectCount(wrapper);
    }
}
